/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.attachment;

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.util.Date;

import javax.swing.Icon;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;

public class TimeAttachment extends ComponentAttachment{
	
    JTextPane textPane = new JTextPane();
    Icon icon = TWaverUtil.getIcon("/demo/network/miscellaneous/instrument/clock_icon.png");
    
	public TimeAttachment(String name, ElementUI ui) {
		super(name, ui);
		this.updateTime();
		this.setStyle(TWaverConst.ATTACHMENT_STYLE_BUBBLE);
		this.setShadowVisible(true);
		this.setBodyVisible(true);
		this.setPosition(TWaverConst.POSITION_INNER_TOPRIGHT);
		this.setXOffset(5);
		this.setYOffset(-8);
		this.setClosable(true);
		this.setMinimizable(true);
		this.setSize(textPane.getPreferredSize());
		this.setComponent(textPane);
	}

	public void elementPropertyChange(PropertyChangeEvent evt) {
		super.elementPropertyChange(evt);
		if((TWaverConst.CLIENT_PROPERTY_PREFIX + "currentTime").equals(evt.getPropertyName())){
			updateTime();
		}
	}

	private void updateTime(){
		Object time = element.getClientProperty("currentTime");
		if(time == null){
			time = new Date();
		}
        try {
    	    StyledDocument doc = textPane.getStyledDocument();
        	doc.remove(0, doc.getLength());
        	
            SimpleAttributeSet set = new SimpleAttributeSet();
            textPane.insertIcon(icon);
            StyleConstants.setBold(set, true);
			doc.insertString(doc.getLength(), " Current Time:\n", set);
			
			set = new SimpleAttributeSet();
			StyleConstants.setForeground(set, Color.RED);
			doc.insertString(doc.getLength(), time.toString(), set);
			
		} catch (BadLocationException e) {
		}
	}

}